package com.mr.qa.action.check;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.mr.qa.GlobalConfigs;

public class DailyAddImageCheck implements DailyCheck {
	
	public static HashMap userImageSize;
    public static String recordDateString;//yyyy-mm-dd

    public void init(int userId){
    	Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userImageSize == null)
        {
        	userImageSize = new HashMap();
            recordDateString = dateString;
            userImageSize.put(userId,0);
        }
    }
    
    public boolean update(int userId){
    	Date calendar = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(calendar);
        boolean ifNewRecord = false;
        if(!dateString.equals(recordDateString) || recordDateString == null)
        {
            ifNewRecord = true;
        }
        if(ifNewRecord || userImageSize == null)
        {
        	userImageSize = new HashMap();
            recordDateString = dateString;
            userImageSize.put(userId,1);
        }
        else
        {
            Integer number = (Integer)userImageSize.get(userId);
            if(number == null)
            	userImageSize.put(userId,1);
            else
            {
                if(number >= GlobalConfigs.NUMBER_IMAGE_A_USER_ONE_DAY)
                    return false;
                else
                {
                	userImageSize.put(userId,number+1);
                }
            }
        }
        return true;
    }
    
    public boolean check(int userId)
    {
            if(userImageSize == null || userImageSize.size() == 0)
                return true;
            Integer number = (Integer)userImageSize.get(userId);
            if(number != null && 
            number >= GlobalConfigs.NUMBER_IMAGE_A_USER_ONE_DAY)
                    return false;
            
            return true;
    }

}
